<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Postings extends Model
{
    protected $table = 'tbl_archive';
    public $timestamps = false;

    ####关联关系 start######
    public function huati()
    {
        return $this->belongsTo(Huati::class, 'huati_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'member_id', 'c_id');
    }

    ####关联关系 end######

    const VIEW_YES = 1;
    const VIEW_NO = 2;

    const HOT_YES = 1;
    const HOT_NO = 0;

    const PAGE_SHOW = 10;

    /**
     * 文章列表
     * @param $huati_id
     * @param $page
     * @return array
     */
    static function PostingsList($huati_id, $page)
    {
        $datas = [];
        if (!$page) {
            $page = 1;
        }
        //获取数据
        $lists = Postings::with('user:c_id,name,c_avatar')->where('huati_id', $huati_id)->where('is_view', self::VIEW_YES);
        //数据量
        $number = $lists->count();
        $pages = ceil($number / self::PAGE_SHOW);
        $lists = $lists->skip(($page - 1) * self::PAGE_SHOW)->take(self::PAGE_SHOW)->orderByDesc('archive_id')->get();
        if ($number > 0) {
            foreach ($lists as $list) {
                if (!$list->user) {
                    continue;
                }
                //有没有视频或者图片
                if ($list->video) {
                    $enclosure = 'video';
                    $files = $list->video;
                } else {
                    $pics = DB::table('tbl_picture')->where('archive_id', $list->archive_id)->pluck('path')->toArray();
                    if (count($pics) > 0) {
                        $enclosure = 'picture';
                        $files = $pics;
                    } else {
                        $enclosure = 'none';
                        $files = '';
                    }
                }
                $ping = DB::table('tbl_article_comment')->where('article_id', $list->archive_id);
                $comment = $ping->count();
                $ping_ids = $ping->pluck('id')->toArray();
                $reply = DB::table('tbl_archive_reply')->whereIn('lou_id', $ping_ids)->count();
                $datas[] = [
                    'user_id' => $list->user->c_id,
                    'user_name' => $list->user->name,
                    'user_avatar' => $list->user->c_avatar,
                    'archive_id' => $list->archive_id,
                    'archive_title' => $list->title,
                    'archive_content' => $list->content,
                    'archive_city' => $list->city,
                    'archive_time' => $list->pub_time,
                    'archive_good_num' => DB::table('tbl_archive_favor')->where('archive_id', $list->archive_id)->count(),//$list->good_num,
                    'archive_ping_num' => $comment + $reply,
                    'archive_keshi' => $list->keshi,
                    'archive_is_hot' => $list->is_hot,
                    'enclosure' => $enclosure,
                    'files' => $files,
                ];
            }
        }

        return success(compact('datas', 'number', 'pages'));
    }

    /**
     * 举报信息
     * @param $huati_id
     * @return array
     */
    static function Report($huati_id)
    {
        $datas = [];
        $page = request('page', 0);
        $postings_ids = Postings::where('huati_id', $huati_id)->pluck('archive_id')->toArray();
        if (count($postings_ids) > 0) {
            $lists = DB::table('jubao')->select('reportArticleId', 'text', 'ctime')->whereIn('reportArticleId', $postings_ids);
            $number = $lists->count();
            $pages = ceil($number / self::PAGE_SHOW);
            $lists = $lists->skip(($page - 1) * self::PAGE_SHOW)->take(self::PAGE_SHOW)->orderByDesc('id')->get();
            if (count($lists) > 0) {
                foreach ($lists as $list) {
                    $datas[] = [
                        'title' => Postings::where('archive_id', $list->reportArticleId)->value('title'),
                        'contents' => $list->text,
                        'time' => $list->ctime,
                    ];
                }
            }
        }
        return success(compact('datas', 'pages'));
    }
}
